package com.hongxin.doc.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;
import java.util.Map;

public class JsonUtil {
    public static String getJson(Object objectMap) {
        try {
            return new ObjectMapper().writeValueAsString(objectMap);
        } catch (Exception e) {
            return "";
        }
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> getMap(String json) {
        if (json.equals("")) {
            return null;
        }
        try {
            return new ObjectMapper().readValue(json, Map.class);
        } catch (Exception e) {
            return null;
        }
    }

    public static List<Map<String, Object>> getList(String json) {
        if (json.equals("")) {
            return null;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<Map<String, Object>> dataList = objectMapper.readValue(json, List.class);
            return dataList;
        } catch (JsonProcessingException e) {
            return null;
        }
    }

}
